//
//  ZhanhuiViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/5/15.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define foot_Height 120 *SizeScale

#define kBtnY App_Frame_Height-kNavbarAndStatusBar-(foot_Height*2/3)

#import "ZhanhuiViewController.h"

#import "HomeShowListModel.h"

#import "BHSingleButton.h"

#import "ZhanquViewController.h"

static NSString *const listCell = @"listCell";

@interface ZhanhuiViewController ()<UITableViewDelegate,UITableViewDataSource,TabelViewRefreshDelegate>
@property (nonatomic, strong) RefreshTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) BHSingleButton *nextBtn;

@property (nonatomic, strong) NSIndexPath *chooseIndex;

@end

@implementation ZhanhuiViewController

#pragma mark - Lazy
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"展会选择";
    [self initNavBarBackBtn];
    [self setUpUI];
    self.view.backgroundColor = Bg_Color_F7F7F7;
    [self.tableView headerRefresh];
}

- (void)setUpUI {
    _tableView = [[RefreshTableView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height-foot_Height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.isRefresh = YES;
    _tableView.refreshDelegate = self;
    _tableView.tableHeaderView = [[UIView alloc]init];
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.scrollsToTop = NO;
    _tableView.backgroundColor = Bg_Color_F7F7F7;
    [self.view addSubview:_tableView];
    
    _nextBtn = [[BHSingleButton alloc] initWithFrame:CGRectMake(10, kBtnY, App_Frame_Width-(10*2), 60)];
    _nextBtn.button_name(@"下一步（3/5）");
    [_nextBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, App_Frame_Width, CGRectGetHeight(self.view.bounds)-foot_Height);
}


#pragma mark - TabelViewRefreshDelegate
- (void)tableView:(UITableView *)tableView Refresh:(BOOL)refresh PageCount:(NSInteger)pageCount {
    [self requestPostBH_Exhibition_getExhibitionByHallId:refresh PageCount:pageCount];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:listCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    HomeShowListModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = [BHSettingUtil dataAndStringIsNull:model.title]?@"未知展会名称":model.title;
    if (self.chooseIndex == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.chooseIndex = indexPath;
    [self.tableView reloadData];
}

- (void)nextAction {
    if([BHSettingUtil dataAndStringIsNull:self.chooseIndex]) {
        [[BHAlertUtil alertManager] showPromptInfo:@"请先选择"];
    }
    else {
        ZhanquViewController *next = [[ZhanquViewController alloc] init];
        HomeShowListModel *model = self.dataArray[self.chooseIndex.row];
        next.Id = model.Id;
        [self.showInfoArray replaceObjectAtIndex:2 withObject:next.Id];
        next.showInfoArray = self.showInfoArray;
        [self pushViewController:next animated:YES];
    }
}

#pragma mark - 接口
- (void)_loadData:(id)response Refresh:(BOOL)refresh {
    if (refresh) [self.dataArray removeAllObjects];
    NSArray *arr = [NSArray yy_modelArrayWithClass:[HomeShowListModel class] json:response];
    [self.dataArray addObjectsFromArray:arr];
    if (arr.count < 10) {
        [self.tableView endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}

- (void)requestPostBH_Exhibition_getExhibitionByHallId:(BOOL)refresh PageCount:(NSInteger)pageCount {
    self.dataArray.count != 0 ?: [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"" forKey:@"vague"];
    [params setObject:self.Id forKey:@"hall_id"];
    [params setObject:[NSString stringWithFormat:@"%ld",(long)pageCount] forKey:@"page"];
    [params setObject:@"10" forKey:@"pagesize"];
    
    [PPHTTPRequest requestPostBH_Exhibition_getExhibitionByHallId:^(id response, BOOL success) {
        if (self.dataArray.count == 0 && [response isKindOfClass:[NSString class]]) {
            //            self.collectionView.placeholderShow(YES).prompt_title((NSString *)response).isButtonHidden(YES);
        }else self.tableView.placeholderShow(NO);
        if (success && ![response isKindOfClass:[NSString class]]) {
            [self _loadData:response Refresh:refresh];
        }
        if (success && refresh == YES) {
            [self _loadData:response Refresh:refresh];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params];
}

@end
